package cn.kgc.vue.controller;

import cn.kgc.vue.common.ResponseResult;
import cn.kgc.vue.dto.StudentDTO;
import cn.kgc.vue.entity.Student;
import cn.kgc.vue.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 课工场
 * @Version: v1.0  2023/7/18
 * @Description:
 */
@RestController
@RequestMapping("student")
@CrossOrigin
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping("list")
    public ResponseResult  getStudentPage(StudentDTO studentDTO){
        return  studentService.getStudentPage(studentDTO);
    }


    @GetMapping("{id}")
    public ResponseResult  getById(@PathVariable("id") Integer id){
        ResponseResult responseResult = new ResponseResult();
        Student student = studentService.getById(id);
        responseResult.setData(student);
        return responseResult ;
    }

    @PostMapping("saveOrUpdate")
    public ResponseResult  saveOrUpdate(@RequestBody Student student){
        ResponseResult responseResult = new ResponseResult();
        //如何识别  添加还是 修改  student  id
        studentService.saveOrUpdate(student);
        responseResult.setCode(0);
        responseResult.setMsg("操作成功");
        return responseResult  ;
    }

    @DeleteMapping("batchDelete")
    public ResponseResult  batchDelete(@RequestBody List<Integer> ids){
        System.out.println("============ids=========== " + ids);
        ResponseResult responseResult = new ResponseResult();
        studentService.removeBatchByIds(ids);
        responseResult.setCode(0);
        responseResult.setMsg("删除成功");
        return responseResult  ;
    }


}
